using UnityEngine;
using System.Collections;

public class MControl : MonoBehaviour, MIControl
{
	public int Id;
    public float x, y, depth;
	public Texture view;
	
	protected bool bDown = false;
	protected TouchPhase touchPhase;
	
	public static float sx = -480 / 2;
	public static float sy = 320 / 2;
	
	protected float tx;
	protected float ty;
	
	public object userdata;
	
    public void Start()
	{
        tx = view.width;
        ty = view.height;

        gameObject.transform.localScale = new Vector3(tx / 10, 1, ty / 10);
        gameObject.transform.position = new Vector3(sx + tx / 2 + x, sy - ty / 2 - y, -depth);
		
        gameObject.renderer.material.mainTexture = view;
        gameObject.renderer.material.mainTextureOffset = new Vector2(1, 1);
        gameObject.renderer.material.mainTextureScale = new Vector2(-1, -1);
	}
	
	public float tX
	{
		get { return tx; }
	}
	
	public float tY
	{
		get { return ty; }
	}
	
	public virtual void Update()
	{
		if( touchPhase == TouchPhase.Moved )
		{
			if( bDown == true )
			{
				bDown = false;
			}
		}
	}
	
	public virtual void OnEvent(Touch t)
	{
		touchPhase = t.phase;
		if( t.phase == TouchPhase.Began )
		{
			OnDown(t);
		}
		else if( t.phase == TouchPhase.Ended )
		{
			OnUp(t);
		}
		else if( t.phase == TouchPhase.Canceled )
		{
			OnUp(t);
		}
	}
	
	public virtual int OnDown(Touch t)
	{
		if( bDown == false )
		{
			bDown = true;
		}
		
		return 0;
	}
	
	public virtual int OnUp (Touch t)
	{
		if( bDown == true )
		{
			bDown = false;
			return 1;
		}
		
		return 0;
	}
}

